﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using Runda.CMS.Common.Exchange;

namespace Runda.CMS.Service.RundaService
{
    public class MailSender
    {
        private static string SMTPServer = AppSettings["SMTPServer"];
        private static int SMTPPort = int.Parse(AppSettings["SMTPPort"]);
        private static string SMTPFrom = AppSettings["From"];
        private static string SMTPFromPwd = AppSettings["FromPwd"];
        private static string SMTPFromDisplay = AppSettings["FromDisplay"];
        private static System.Collections.Specialized.NameValueCollection AppSettings { get { return System.Configuration.ConfigurationManager.AppSettings; } }


        public static bool SendEmail(string from, string fromDisplay, string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
        {
            bool isSuccess = false;

            try
            {
                SmtpClient client = new SmtpClient(SMTPServer);
                client.Credentials = new System.Net.NetworkCredential(SMTPFrom, SMTPFromPwd);

                //client.EnableSsl = true;
                MailAddress fromAddr = new MailAddress(from, fromDisplay);

                // Set destinations for the e-mail message.
                MailAddress toAddr = new MailAddress(to, toDisplay);

                // Specify the message content.
                MailMessage message = new MailMessage(fromAddr, toAddr);

                if (cc != string.Empty)
                {
                    MailAddress ccAddr = new MailAddress(cc, ccDisplay);
                    message.CC.Add(ccAddr);
                }

                message.IsBodyHtml = true;
                message.Body = body;
                message.BodyEncoding = System.Text.Encoding.UTF8;

                message.Subject = subject;
                message.SubjectEncoding = System.Text.Encoding.UTF8;

                if (attachments != null)
                {
                    foreach (MailAttachment mailAttach in attachments)
                    {
                        if (mailAttach.AttachmentData != null)
                        {
                            Attachment attach = new Attachment(mailAttach.AttachmentData, mailAttach.AttachmentName);

                            message.Attachments.Add(attach);
                        }
                    }
                }
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                client.Send(message);

                message.Dispose();

                isSuccess = true;
                return isSuccess;
            }
            catch (Exception ex)
            {
                try
                {
                    isSuccess = false;
                }
                catch
                {
                    isSuccess = false;
                }
                return isSuccess;
            }
        }


        public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body)
        {
            return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body, null);
        }

        public static bool SendEmail(string to, string toDisplay, string cc, string ccDisplay, string subject, string body, List<MailAttachment> attachments)
        {
            return SendEmail(SMTPFrom, SMTPFromDisplay, to, toDisplay, cc, ccDisplay, subject, body, attachments);
        }
    }
}
